﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class ProductSystemsCollection : AbstractCollection
    {
        /// <summary>
        /// Стaтическая коллекция 
        /// </summary>
        protected static List<ProductSystems> _list;

        /// <summary>
        /// Список объектов
        /// </summary>
        public List<ProductSystems> ProductSystemsList
        {
            get
            {
                return Entities.ProductSystems.ToList();
                if (_list == null)
                    _list = DB.Entities.ProductSystems.ToList();
                return _list;
            }
        }

        /// <summary>
        /// список активных объектов
        /// </summary>
        public List<ProductSystems> ProductSystemsActiveList
        {
            get
            {
                return Entities.ProductSystems.Where(c => c.IsActive == true).ToList();
                var result = new List<ProductSystems>();
                if (ProductSystemsList.Count < 1) return result;
                foreach (ProductSystems item in ProductSystemsList)
                {
                    if (item.IsActive != null)
                        if (item.IsActive == true)
                            result.Add(item);
                }
                return result;
            }
        }

        #region Overrides of AbstractCollection

        /// <summary>
        /// Обновление элемента в коллекции
        /// </summary>
        /// <param name="item">Новый элемент</param>
        /// <param name="userLogin">Логин пользователя, который делает изменения</param>
        /// <returns>Результат операции</returns>
        public bool Update(object item, string userLogin)
        {
            var newObject = (ProductSystems) item;
            if (newObject == null)
                return false;

            var object4Update = GetItemByIdFromDatabase(newObject.GetKeyValue()) as ProductSystems;
            if (object4Update == null)
                return false;

            if (!string.IsNullOrEmpty(newObject.ProductSystemRu))
                object4Update.ProductSystemRu = newObject.ProductSystemRu;

            if (!string.IsNullOrEmpty(newObject.ProductSystemEn))
                object4Update.ProductSystemEn = newObject.ProductSystemEn;

            if (!string.IsNullOrEmpty(newObject.ProductSystemDe))
                object4Update.ProductSystemDe = newObject.ProductSystemDe;

            if (!string.IsNullOrEmpty(newObject.ProductSystemSAP))
                object4Update.ProductSystemSAP = newObject.ProductSystemSAP;

            object4Update.IdProductMark = newObject.IdProductMark;

            object4Update.IsActive = newObject.IsActive;
            return base.Update(object4Update, userLogin);
        }

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(ProductSystemsList);
        }

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (ProductSystems);
        }

        #endregion

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
            _list = null;
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            return _list;
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.ProductSystems.Find(itemId);
        }

        /// <summary>
        /// Получить список систем клиента
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public List<ProductSystems> GetProdustSystemsListByCustomer(Customers customer)
        {
            var result = new List<ProductSystems>();
            if (customer == null) return result;
            if (customer.CustomersProductsSystems == null) return result;
            foreach (CustomersProductsSystems item in customer.CustomersProductsSystems)
            {
                if (item.ProductSystems != null)
                    result.Add(item.ProductSystems);
            }
            return result;
        }

        /// <summary>
        /// Получить список систем клиента
        /// </summary>
        /// <param name="idCustomer"></param>
        /// <returns></returns>
        public List<ProductSystems> GetProdustSystemsListByCustomerId(int idCustomer)
        {
            var result = new List<ProductSystems>();
            if (idCustomer < 1) return result;
            var bindingCol = new CustomersProductSystemsCollection();
            var productsSystemsCol = new ProductSystemsCollection();
            if (bindingCol.CustomersProductSystemsList.Count < 1) return result;
            foreach (CustomersProductSystems bindingItem in bindingCol.CustomersProductSystemsList)
            {
                if (bindingItem.IdCustomer == idCustomer)
                {
                    var productSystm = (ProductSystems) productsSystemsCol.GetItemById(bindingItem.IdProductSystem);
                    if (productSystm != null)
                    {
                        result.Add(productSystm);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// получить список систем по ИД заявки
        /// </summary>
        /// <param name="idRequest"></param>
        /// <returns></returns>
        public List<ProductSystems> GetProductSystemsListByRequestId(int idRequest)
        {
            var result = new List<ProductSystems>();
            var bindingCol = new TechnicRequestsProductSystemsCollection();
            if (bindingCol.TechnicRequestsProductSystemsList.Count < 1) return result;
            foreach (TechnicRequestsProductSystems item in bindingCol.TechnicRequestsProductSystemsList)
            {
                if (item.IdTechnicRequest == idRequest)
                {
                    if (item.ProductSystems != null)
                        result.Add(item.ProductSystems);
                }
            }
            return result;
        }
    }
}